// @ts-nocheck
import React from 'react';
import {
  Intent,
  Button,
  ButtonGroup,
  Popover,
  PopoverInteractionKind,
  Position,
  Menu,
  MenuItem,
} from '@blueprintjs/core';
import { useFormikContext } from 'formik';
import { FormattedMessage as T, PageForm, Group } from '@/components';
import { useHistory } from 'react-router-dom';

import { Icon, If } from '@/components';
import { useExpenseFormContext } from './ExpenseFormPageProvider';

/**
 * Expense form floating actions.
 */
export default function ExpenseFloatingFooter() {
  const history = useHistory();

  // Formik context.
  const { isSubmitting, submitForm, resetForm } = useFormikContext();

  // Expense form context.
  const { setSubmitPayload, isNewMode } = useExpenseFormContext();

  // Handle submit & publish button click.
  const handleSubmitPublishBtnClick = (event) => {
    setSubmitPayload({ redirect: true, publish: true });
    submitForm();
  };

  // Handle submit, publish & new button click.
  const handleSubmitPublishAndNewBtnClick = (event) => {
    setSubmitPayload({ redirect: false, publish: true, resetForm: true });
    submitForm();
  };

  // Handle submit, publish & continue editing button click.
  const handleSubmitPublishContinueEditingBtnClick = (event) => {
    setSubmitPayload({ redirect: false, publish: true });
    submitForm();
  };

  // Handle submit as draft button click.
  const handleSubmitDraftBtnClick = (event) => {
    setSubmitPayload({ redirect: true, publish: false });
    submitForm();
  };

  // Handle submit as draft & new button click.
  const handleSubmitDraftAndNewBtnClick = (event) => {
    setSubmitPayload({ redirect: false, publish: false, resetForm: true });
    submitForm();
  };

  // Handles submit as draft & continue editing button click.
  const handleSubmitDraftContinueEditingBtnClick = (event) => {
    setSubmitPayload({ redirect: false, publish: false });
    submitForm();
  };

  // Handle cancel button click.
  const handleCancelBtnClick = (event) => {
    history.goBack();
  };

  // Handles clear form button click.
  const handleClearBtnClick = (event) => {
    resetForm();
  };

  return (
    <PageForm.FooterActions spacing={10} position="apart">
      <Group spacing={10}>
        {/* ----------- Save And Publish ----------- */}
        <If condition={isNewMode}>
          <ButtonGroup>
            <Button
              disabled={isSubmitting}
              loading={isSubmitting}
              intent={Intent.PRIMARY}
              onClick={handleSubmitPublishBtnClick}
              text={<T id={'save_publish'} />}
            />
            <Popover
              content={
                <Menu>
                  <MenuItem
                    text={<T id={'publish_and_new'} />}
                    onClick={handleSubmitPublishAndNewBtnClick}
                  />
                  <MenuItem
                    text={<T id={'publish_continue_editing'} />}
                    onClick={handleSubmitPublishContinueEditingBtnClick}
                  />
                </Menu>
              }
              minimal={true}
              interactionKind={PopoverInteractionKind.CLICK}
              position={Position.BOTTOM_LEFT}
            >
              <Button
                disabled={isSubmitting}
                intent={Intent.PRIMARY}
                rightIcon={<Icon icon="arrow-drop-up-16" iconSize={20} />}
              />
            </Popover>
          </ButtonGroup>
          {/* ----------- Save As Draft ----------- */}
          <ButtonGroup>
            <Button
              disabled={isSubmitting}
              className={'ml1'}
              onClick={handleSubmitDraftBtnClick}
              text={<T id={'save_as_draft'} />}
            />
            <Popover
              content={
                <Menu>
                  <MenuItem
                    text={<T id={'save_and_new'} />}
                    onClick={handleSubmitDraftAndNewBtnClick}
                  />
                  <MenuItem
                    text={<T id={'save_continue_editing'} />}
                    onClick={handleSubmitDraftContinueEditingBtnClick}
                  />
                </Menu>
              }
              minimal={true}
              interactionKind={PopoverInteractionKind.CLICK}
              position={Position.BOTTOM_LEFT}
            >
              <Button
                disabled={isSubmitting}
                rightIcon={<Icon icon="arrow-drop-up-16" iconSize={20} />}
              />
            </Popover>
          </ButtonGroup>
        </If>
        {/* ----------- Save and New ----------- */}
        <If condition={!isNewMode}>
          <ButtonGroup>
            <Button
              disabled={isSubmitting}
              loading={isSubmitting}
              intent={Intent.PRIMARY}
              onClick={handleSubmitPublishBtnClick}
              style={{ minWidth: '85px' }}
              text={<T id={'save'} />}
            />
            <Popover
              content={
                <Menu>
                  <MenuItem
                    text={<T id={'save_and_new'} />}
                    onClick={handleSubmitPublishAndNewBtnClick}
                  />
                </Menu>
              }
              minimal={true}
              interactionKind={PopoverInteractionKind.CLICK}
              position={Position.BOTTOM_LEFT}
            >
              <Button
                disabled={isSubmitting}
                intent={Intent.PRIMARY}
                rightIcon={<Icon icon="arrow-drop-up-16" iconSize={20} />}
              />
            </Popover>
          </ButtonGroup>
        </If>
        {/* ----------- Clear & Reset----------- */}
        <Button
          className={'ml1'}
          disabled={isSubmitting}
          onClick={handleClearBtnClick}
          text={!isNewMode ? <T id={'reset'} /> : <T id={'clear'} />}
        />
        {/* ----------- Cancel ----------- */}
        <Button
          className={'ml1'}
          onClick={handleCancelBtnClick}
          text={<T id={'cancel'} />}
        />
      </Group>
    </PageForm.FooterActions>
  );
}
